import dataclasses
from dataclasses import dataclass, field
from typing import List, Dict
import os
import json

@dataclass
class TestCase:
    name: str
    result: str
    time: int
    classname: str
    status: str = "RUN"


@dataclass
class TestSuite:
    name: str
    tests: int
    failures: int
    disabled: int
    time: int
    errors: int
    testsuite: List[TestCase]


@dataclass
class AllTest:
    name: str
    tests: int
    failures: int
    disabled: int
    time: int
    testsuites: List[TestSuite]
    errors: int = 0


class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if dataclasses.is_dataclass(obj):
            return dataclasses.asdict(obj)
        return super().default(obj)


def file_names_in_dir(path: str) -> list[str]:
    for _, _, fs in os.walk(path):
        return list(fs)
    return []


def dir_names_in_dir(path: str) -> list[str]:
    for _, ds, _ in os.walk(path):
        return list(ds)
    return []


def is_test_dir(path: str) -> bool:
    filenames = file_names_in_dir(path)
    return "results.json" in filenames


def get_all_utest_dirs(path: str) -> list[str]:
    if is_test_dir(path):
        return [path]

    result = []

    for name in dir_names_in_dir(path):
        d = os.path.join(path, name)
        if is_test_dir(d):
            result.append(d)
        else:
            result.extend(get_all_utest_dirs(d))

    return result

